﻿using aier.dataaccess.model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace aier.dataaccess.dapper
{
    /// <summary>
    /// 系统参数配置服务实现类
    /// </summary>
    public class SysParaDal
    {
        public DapperHelper dapperHelper { get; set; }

        /// <summary>
        /// 获取所有参数列表信息  
        /// 对应原程序 getAll()
        /// </summary>
        /// <returns></returns>
        public List<SysPara> GetAll()
        {
            SqlBuilder sqlBuilder = new SqlBuilder();

           return dapperHelper.ExecueWithoutTransaction<List<SysPara>>(a =>
            {
                string sql = @"select distinct * from AIER_T_SYS_PARA";
                var sqlTemplate = sqlBuilder.AddTemplate(sql);

                return a.Query<SysPara>(sqlTemplate.RawSql).ToList();
            });

        }

        /// <summary>
        /// 根据参数类型与代码获取参数对像
        /// 对应原程序 getById
        /// </summary>
        /// <param name="paratype">参数类型</param>
        /// <param name="paracode">参数代码</param>
        /// <returns></returns>
        public SysPara GetById(string paratype, string paracode)
        {
            if (!string.IsNullOrEmpty(paratype) && !string.IsNullOrEmpty(paracode))
            {
                SysPara sysPara = null;
                SqlBuilder sqlBuilder = new SqlBuilder();
                DynamicParameters sqlParams = new DynamicParameters();

                dapperHelper.ExecueWithoutTransaction(a =>
                {
                    string sql = @"select distinct * from AIER_T_SYS_PARA /**where**/ ";
                    var sqlTemplate = sqlBuilder.AddTemplate(sql);
                    sqlBuilder.Where("new_paratype=@new_paratype and new_paracode = @new_paracode ");
                    sqlParams.Add("@new_paratype", paratype);
                    sqlParams.Add("@new_paracode", paracode);

                    sysPara = a.QueryFirstOrDefault<SysPara>(sqlTemplate.RawSql, sqlParams);
                });

                return sysPara;
            }

            return null;
        }

    }
}
